package br.com.ledger.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import br.com.ledger.containers.components.AbstractBalancete;
import br.com.ledger.model.RazoneteBean;
import br.com.ledger.utils.JTextFieldLimit;

@SuppressWarnings("serial")
public class TelaSetRazonete extends JDialog implements KeyListener {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtNome;
	private RazoneteBean razonete;
	private JButton okButton, cancelButton;
	private JComboBox boxNaturezas;

	/**
	 * 
	 * 
	 * 
	 */
	public TelaSetRazonete(RazoneteBean razonete) {
		this.razonete = razonete;
		
		setTitle((razonete == null) ? "Adicionar" : "Alterar");
		
		setResizable(false);
		setModal(true);
		setBounds(100, 100, 450, 246);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(245, 245, 245));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblNomenclatura = new JLabel("Nomenclatura:");
		lblNomenclatura.setBounds(12, 25, 147, 16);
		contentPanel.add(lblNomenclatura);
		
		txtNome = new JTextField();
		txtNome.setDocument(new JTextFieldLimit(50));
		txtNome.setBounds(12, 40, 229, 25);
		contentPanel.add(txtNome);
		txtNome.setColumns(10);
		txtNome.addKeyListener(this);
		
		JLabel lblNatureza = new JLabel("Natureza:");
		lblNatureza.setBounds(12, 77, 55, 16);
		contentPanel.add(lblNatureza);
		
		boxNaturezas = new JComboBox(AbstractBalancete.naturezas);
		boxNaturezas.setBounds(12, 97, 229, 25);
		contentPanel.add(boxNaturezas);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBorder(new MatteBorder(1, 0, 0, 0, (Color) Color.GRAY));
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.setBackground(new Color(220, 220, 220));
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setRazonete();
					}
				});
				okButton.setPreferredSize(new Dimension(80, 35));
				okButton.setActionCommand("OK");
				okButton.setEnabled(false);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancelar");
				cancelButton.setBackground(new Color(220, 220, 220));
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						TelaSetRazonete.this.razonete = null;
						dispose();
					}
				});
				cancelButton.setPreferredSize(new Dimension(80, 35));
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		setLocationRelativeTo(null);
		
		if (razonete != null) {
			txtNome.setText(razonete.getNome());
			boxNaturezas.setSelectedItem(razonete.getNatureza());
			okButton.setEnabled(true);
		}
		
		setVisible(true);
	}
	
	private void setRazonete() {
		razonete = new RazoneteBean(txtNome.getText());
		razonete.setNatureza(String.valueOf(boxNaturezas.getSelectedItem()));
		dispose();
	}
	
	public RazoneteBean getRazonete() {
		return this.razonete;
	}

	@Override
	public void keyPressed(KeyEvent arg0) {	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		if (!txtNome.getText().trim().equals(""))
			okButton.setEnabled(true);
		else
			okButton.setEnabled(false);
	}

	@Override
	public void keyTyped(KeyEvent arg0) { }
}
